package com.meiyu.live.ui.main;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.eventbus.NowLiveUpdateEvent;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.param.MeiyuListParams;
import com.meiyu.live.net.response.HookupsResponse;
import com.meiyu.live.net.response.HotLiveResponse;
import com.meiyu.live.ui.main.widget.NowHotLiveView;
import com.meiyu.live.ui.main.widget.NowHotSoonView;

import de.greenrobot.event.EventBus;

/**
 * 正在热播
 */
public class NowLiveFragment extends BaseFragment {

    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private RecommendListAdapter adapter;
    private Handler handler = new Handler();

    private NowHotLiveView nowHotLiveView;
    private NowHotSoonView nowHotSoonView;

    public NowLiveFragment() {
    }

    public static NowLiveFragment newInstance() {
        NowLiveFragment fragment = new NowLiveFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getContextView() {
        return R.layout.fragment_columnall;
    }

    @Override
    protected void init() {
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        nowHotLiveView = new NowHotLiveView(getActivity());
        nowHotSoonView = new NowHotSoonView(getActivity());
        listView = (ListView) findViewById(R.id.listview);
        adapter = new RecommendListAdapter();
        listView.setAdapter(adapter);

        getData(true);
    }

    private void getData(final boolean reload) {
        //正在播出（新版本）
        int area[] = {6, 6};
        MeiyuListParams pageParams = new MeiyuListParams(1, area);
        pageParams.setUrl(Constant.UrlParams.MEIYU_LIST);
        request(pageParams, HotLiveResponse.class, new Response.Listener<HotLiveResponse>() {
            @Override
            public void onResponse(HotLiveResponse indexAllResponse) {
                refreshLayout.setRefreshing(false);
                if (indexAllResponse != null) {
                    nowHotLiveView.setData(indexAllResponse.getHash());
//                    adapter.notifyDataSetChanged();
                }
            }
        });

        //即将播出
        BaseBodyParams b = new BaseBodyParams();
        b.setUrl(Constant.VERSION + Constant.UrlParams.DOING);
        request(b, HookupsResponse.class, new Response.Listener<HookupsResponse>() {
            @Override
            public void onResponse(HookupsResponse hookupsResponse) {
                refreshLayout.setRefreshing(false);
                if (hookupsResponse != null) {
                    nowHotSoonView.setData(hookupsResponse.getSuccess());
//                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        refreshLayout.setRefreshing(false);
        super.onErrorResponse(volleyError);
    }

    class RecommendListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            switch (getItemViewType(position)) {
                case 0:
                    convertView = nowHotLiveView;
                    break;
                case 1:
                    convertView = nowHotSoonView;
                    break;
            }
            return convertView;
        }
    }

    public void onEvent(NowLiveUpdateEvent event){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData(true);
            }
        },1500);
    }
}
